<?php
// If not yet configured, go to the installation page
if (!file_exists("db_config.inc")) {
    header('Location: install/install.php');
    die();
}

$is_index = true; // to do not denied access to the page when not logged


////set root_path and root_url in session
//if (basename($_SERVER["SCRIPT_FILENAME"]) == "index.php") {
//    if (!isset($_SESSION["root_path"]))
//        $_SESSION["root_path"] = dirname($_SERVER["SCRIPT_FILENAME"]);
//    if (!isset($_SESSION["root_url"]))
//        $_SESSION["root_url"] = dirname($_SERVER["PHP_SELF"]);
//}

include "header.inc";

// handle login request
if (!isset($_SESSION["user"]) && isset($_POST["username"]) && isset($_POST["password"])) {
    require_once "DataBase.inc";
    $result = DataBase::select("staff", null, array("username"=>$_POST["username"]));
    if ($result <> false && $result[0]["password"] == md5($_POST["password"])) {
        $_SESSION["user"] = $_POST["username"];
        $_SESSION["user_first_name"] = $result[0]["first_name"];
        $_SESSION["user_last_name"] = $result[0]["last_name"];
        $result = DataBase::select("staff_roles",null,array("username"=>$_POST["username"]));
        $roles = array();
        if ($result <> false) foreach ($result as $r) array_push($roles, $r["role"]);
        $_SESSION["user_roles"] = $roles;
        header('Location: index.php');
        die();
    }
}
//sophea
//if (!isset($_SESSION["user"])) {
//   header("Location:login.php")or die(mysql_error());exit();
//}
// session timeout after 1 hour
if (isset($_SESSION["session_timestamp"]) && time()-$_SESSION["session_timestamp"]>(1*60*60)) unset($_SESSION["user"]);
// if not logged, show login page
if (!isset($_SESSION["user"])) {
    include "login.inc";
    include "footer.inc";
    die();
}
$_SESSION["session_timestamp"] = time();
// check user can access to the application
if (!in_array("application_access",$_SESSION["user_roles"])) {
    include "staff/other_staff.inc";
    include "footer.inc";
    die();
}
// handle selection campaign selection
if (isset($_POST["campaign_id"])) {
    $_SESSION["campaign_id"] = $_POST["campaign_id"];
    require_once("DataBase.inc");
    $result = DataBase::query("SELECT * FROM selection_campaign LEFT JOIN selection_campaign_programs ON selection_campaign_programs.selection_campaign_id=selection_campaign.selection_campaign_id LEFT JOIN program ON program.program_id=selection_campaign_programs.program_id WHERE selection_campaign_programs.selection_campaign_id=".$_SESSION["campaign_id"]);
    $campaign_name = "";
    if ($result<>false && count($result)>0) {
        $s = "";
        foreach ($result as $p) {
            if (strlen($s)>0) $s.=" + ";
            $s .= $p["program_name"];
        }
        $campaign_name = $result[0]["selection_campaign_name"]." (".$s.")";
    }
    $_SESSION["campaign_name"] = $campaign_name;
    header('Location: index.php');
    die();
}
// if no selection campaign selected, go to the campaign page
if (!isset($_SESSION["campaign_id"])) {
    include "select_campaign.inc";
    include "footer.inc";
    die();
}

// --- Main page ---

require_once("DataBase.inc");
$now = time();
DataBase::query("DELETE FROM concurrency WHERE timestamp < ".($now-(12*60*60)));
require_once("general_queries.inc");
?>
<a href='staff/change_password.php'>Change my password</a><br/>
<br style='font-size:4pt'/>

<span style='font-size:16pt;font-weight:bold'>Main screens</span><br/>
<ul style='margin:3px'>
    <li><a href='stats/general.php'>Statistics</a></li>
    <li><a href='applicants.php?view=general'>List of applicants</a></li>
    <li><a href='staff/list.php'>Staff list and activities</a></li>
</ul>

<span style='font-size:16pt;font-weight:bold'>Selection Process - Step by Step</span><br/>
<br/>
<ul style='margin:0px'>
    <li>Status: <?php
        $nbApplicants = query_all_applicants()->count();
        $nbNotExcluded = query_not_excluded_applicants()->count();
        echo $nbNotExcluded."/".$nbApplicants." applicant(s) still in the process (not excluded)";
        ?>
    </li>
    <li>After Information Session
        <ul>
            <li><a href="import_data_entry_operator_file.php">Import Excel Files from Data Entry Operators</a></li>
            <?php if ($nbApplicants>0) {?>
            <li><a href="check_typing.php">Review lists of high school, provinces... to check typing mistakes</a></li>
            <li><a href='applicants.php?view=from_data_entry_operators'>View list of applicants</a></li>
                <?php } ?>
        </ul>
    </li>
    <?php if ($nbApplicants>0) {
        require_once("exams/exam_queries.inc");
        ?>
    <li>Prepare written exams:
        <ul>
            <li>
                <b>Status:</b><ul>
                    <li>
                            <?php $nb_have_exam_plan = query_applicants_with_exam_plan()->count();?>
                            <?php $nb_have_no_exam_plan = query_applicants_without_exam_plan()->count();?>
                        <span style='color:<?php echo ($nb_have_no_exam_plan==0?"green":"red")?>'><?php echo $nb_have_exam_plan?> applicants are assigned to a room and have an exam plan, <?php echo $nb_have_no_exam_plan?> still don't have</span>
                    </li>
                    <li>
                            <?php $nb_centers = query_exam_centers()->count();?>
                            <?php $nb_centers_with_room = query_exam_centers_having_rooms()->count();?>
                            <?php $nb_centers_with_room_and_exam_plan = query_exam_centers_having_rooms_and_exam_plan()->count();?>
					There is <?php echo $nb_centers?> exam centers for this selection campaign, 
                        <span style='color:<?php echo ($nb_centers_with_room == $nb_centers ? "green":"red")?>'><?php echo $nb_centers_with_room?> have rooms specified</span>,
                        <span style='color:<?php echo ($nb_centers_with_room_and_exam_plan == $nb_centers ? "green":"red")?>'>and <?php echo $nb_centers_with_room_and_exam_plan?> have also exam plan (with date and time)</span>
                    </li>
                    <li>
                            <?php $nb_exam_without_supervisor =	query_exam_rooms_needing_supervisor()->count();?>
                        <span style='color:<?php echo ($nb_exam_without_supervisor==0?"green":"red")?>'><?php echo $nb_exam_without_supervisor?> exam rooms (with exam plan and assigned applicants) do not have a supervisor</span>
                    </li>
                </ul>
            </li>
            <li><a href='exams/review_exam_centers.php'>Specify rooms for each center, Plan exams, Assign applicants and supervisors to rooms</a></li>
        </ul>
    </li>
        <?php if ($nb_have_exam_plan>0) {?>
    <li>After Written Exam
        <ul>
            <li><?php include("exams/eligibility/status.inc")?></li>
            <li>Enter attendance for written exam in <select id='attendance_exam_center'>
                            <?php
                            $q = query_exam_centers_info_if_applicants();
                            $q->select("COUNT(applicant.applicant_id)");
                            $q->select("exam_center.*");
                            $q->select("province.province_name");
                            $exam_centers_with_applicants = $q->execute();
                            foreach ($exam_centers_with_applicants as $center) {
                                $nb = $center[0];
                                if ($nb > 0)
                                    echo "<option value='".$center["exam_center_id"]."'>".$center["province_name"].": ".$center["exam_center_name"]." (".$nb." applicants)</option>";
                            }
                            ?>
                </select> <button onclick='enter_written_exam_attendance();return false'>Go</button>
            </li>
            <li><a href='exams/import_amc.php'>Import exam results</a></li>
            <li><a href='exams/results_overview.php'>Results Overview</a></li>
            <li><a href='exams/eligibility/define_rules.php'>Define eligibility rules for each program</a></li>
        </ul>
    </li>
<!--
    <li>Interview
        <ul>
            <li><?php
//            include "interview/status.inc"?></li>
            <li><a href='interview/centers.php'>Specify Interview Centers, rooms, and plan interviews with interviewers and observers</a></li>
            <li>Enter attendance for interview in <select id='attendance_interview_center'>
                            <?php
//                            $q = new Query("applicant");
//                            $q->join_applicant("applicant_interview");
//                            $q->where("applicant_interview.interview_id IS NOT NULL");
//                            $q->select("DISTINCT applicant_interview.interview_center_id");
//                            $q->join("interview_center",null,array("interview_center.interview_center_id=applicant_interview.interview_center_id"));
//                            $q->join("province",null,array("province.province_id=interview_center.province_id"));
//                            $q->select("interview_center.interview_center_name");
//                            $q->select("province.province_name");
//                            $q->order("province.province_name,interview_center.interview_center_name");
//                            $interview_centers_with_applicants = $q->execute();
//                            foreach ($interview_centers_with_applicants as $center) {
//                                echo "<option value='".$center["interview_center_id"]."'>".$center["province_name"].": ".$center["interview_center_name"]."</option>";
//                            }
                            ?>
                </select> <button onclick='enter_interview_attendance();return false'>Go</button>
            </li>
            <li>Enter Interview Results <select id='attendance_interview_result'>
                            <?php
//
//                            $q = new Query("applicant");
//                            $q->join_applicant("applicant_interview");
//
//                            $q->where("applicant_interview.interview_id IS NOT NULL");
//                            $q->select("DISTINCT applicant_interview.interview_center_id");
//                            $q->join("interview_center",null,array("interview_center.interview_center_id=applicant_interview.interview_center_id"));
//                            $q->join("province",null,array("province.province_id=interview_center.province_id"));
//                            $q->select("interview_center.interview_center_name");
//                            $q->select("province.province_name");
//                           // $q->select("interviewer.interviewer_username");
//                            $q->order("province.province_name,interview_center.interview_center_name");
//                            $interview_centers_with_applicants = $q->execute();
//                            foreach ($interview_centers_with_applicants as $center) {
//                                echo "<option value='".$center["interview_center_id"]."'>".$center["province_name"].": ".$center["interview_center_name"]."</option>";
//                            }
                            ?>
                </select> <button onclick='enter_interview_result();return false'>Go</button>
            </li>
            <li><a href='interview/eligibility/define_rules.php'>Define eligibility rules for interview</a></li>
        </ul>
    </li>
    <li>Social Investigation</li>-->
            <?php }?>
        <?php }?>
</ul>

<br/>
<?php require_once("applicants/view_script.inc");?>
<script type='text/javascript'>
    function element(id) {
        return document.all ? document.all[id] : document.getElementById(id);
    }
    function enter_written_exam_attendance() {
        var elem = element('attendance_exam_center');
        var center_id = elem.value;
        if (center_id == null || center_id == 0) { alert('Please select an exam center'); return; }
        view_applicants({
            "applicant.applicant_selection_id":false,
            "applicant.applicant_firstname":false,
            "applicant.applicant_lastname":false,
            "applicant.applicant_khmer_name":false,
            "applicant.sex":false,
            "applicant_exam_room_beta.exam_center_id#province_id#province_name":false,
            "applicant_exam_room_beta.exam_center_id#exam_center_name":false,
            "applicant_exam_room_beta.room_number":false,
            "applicant_more_info.applicant_more_info_attendee_exam_status":true
        },{
            "applicant_exam_room_beta.exam_center_id#exam_center_name":"='"+center_id+"'"
        }, null, "exams/apply_attendance_to_all.inc");
    }
    function enter_interview_attendance() {
        var elem = element('attendance_interview_center');
        var center_id = elem.value;
        if (center_id == null || center_id == 0) { alert('Please select an interview center'); return; }
        view_applicants({
            "applicant.applicant_selection_id":false,
            "applicant.applicant_firstname":false,
            "applicant.applicant_lastname":false,
            "applicant.applicant_khmer_name":false,
            "applicant.sex":false,
            "applicant_interview.interview_center_id#province_id#province_name":false,
            "applicant_interview.interview_center_id#interview_center_name":false,
            "applicant_interview.interview_id#room":false,
            "applicant_more_info.applicant_interview_attendance":true,
            "applicant.excluded":true,
            "applicant.excluded_reason":true
        },{
            "applicant_interview.interview_center_id#interview_center_name":"='"+center_id+"'"
        }, null, "interview/apply_attendance_to_all.inc");
    }
    function enter_interview_result() {
        var elem = element('attendance_interview_center');
        var center_id = elem.value;
        if (center_id == null || center_id == 0) { alert('Please select an interview center'); return; }
        view_applicants({
            "applicant.applicant_selection_id":false,
            "applicant.applicant_firstname":false,
            "applicant.applicant_lastname":false,
            "applicant.applicant_khmer_name":false,
            "applicant.sex":false,
            "applicant_interview.interview_id#interviewer_username":false,
            "applicant_interview.interview_center_id#province_id#province_name":false,
            "applicant_interview.interview_center_id#interview_center_name":false,
            "applicant_interview.interview_id#room":false,
            "applicant_interview.interview_communication_and_maturity":true,
            "applicant_interview.interview_motivation_for_cist":true,
            "applicant_interview.interview_affinities_with_computer_or_carear_plan":true
        },{
            "applicant_interview.interview_center_id#interview_center_name":"='"+center_id+"'",
            "applicant_more_info.applicant_interview_attendance":"='1'"
        }, null, null);
    }
</script>
<?php 
//include("footer.inc");
?>
